package com.smily.excel;

import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.File;
import java.io.IOException;

/**
 * POI解析EXCEL文件
 * Created by Smily on 10/17/15.
 */
public class PoiReadExcel {
    public static void main(String[] args){

        File file = new File("/Users/Smily/Documents/Coding/idae_output/poi_test.xls");
        try {
            HSSFWorkbook workbook = new HSSFWorkbook(FileUtils.openInputStream(file));
            //获取sheet页的两种方式
            HSSFSheet sheet = workbook.getSheet("sheet0");
            HSSFSheet sheet1 = workbook.getSheetAt(0);
            HSSFRow row0 = sheet.getRow(0);
            for (int j = 0; j < row0.getLastCellNum(); j++) {
                HSSFCell cell = row0.getCell(j);
                String value = cell.getStringCellValue();
                System.out.print(value + " ");
            }
            System.out.println();
            for (int i = 1; i < sheet.getLastRowNum(); i++) {
                HSSFRow row = sheet.getRow(i);
                for (int j = 0; j < 3; j++) {
                    HSSFCell cell = row.getCell(j);
                    String value = cell.getStringCellValue();
                    System.out.print(value + " ");
                }
                for (int j = 3; j < row.getLastCellNum(); j++) {
                    HSSFCell cell = row.getCell(j);
                    double value1 = cell.getNumericCellValue();
                    System.out.print(value1 + " ");
                }
                System.out.println();
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
